package freecomm.study.servicetest;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    protected static final String SERVICE_ACTION = "freecomm.study.servicetest.intent.action.SERVICE_ACTION";
    private Button startButton;
    private Button stopButton;
    private Button bindButton;
    private Button unbindButton;
    private IPerson iperson;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = (Button) findViewById(R.id.startBtn);
        stopButton = (Button) findViewById(R.id.stopBind);
        bindButton = (Button) findViewById(R.id.bindBtn);
        unbindButton = (Button) findViewById(R.id.unbindBtn);
        startButton.setOnClickListener(startListener);
        stopButton.setOnClickListener(stopListener);
        bindButton.setOnClickListener(bindListener);
        unbindButton.setOnClickListener(unbindListener);
    }

    private ServiceConnection conn = new ServiceConnection()
    {

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            // TODO Auto-generated method stub
            Log.i("Test", "Connected Successful");
            Toast.makeText(MainActivity.this, "Connected Successful", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            // TODO Auto-generated method stub
            iperson = IPerson.Stub.asInterface(service);
            if(iperson!=null){
                try{
                    iperson.setName("Junwei.xu");
                    iperson.setAge(23);
                    String msg = iperson.display();
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                }catch(RemoteException e){
                    e.printStackTrace();
                }
            }
        }
    };

    private OnClickListener startListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setAction(SERVICE_ACTION);
            startService(intent);
        }
    };
    private OnClickListener stopListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setAction(SERVICE_ACTION);
            stopService(intent);
        }
    };
    private OnClickListener bindListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setAction(SERVICE_ACTION);
            bindService(intent, conn, Service.BIND_AUTO_CREATE);
        }
    };
    private OnClickListener unbindListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setAction(SERVICE_ACTION);
            unbindService(conn);
        }
    };
}